﻿namespace TvUtil
{

	using System;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Drawing;
	using System.Data;
	using System.Text;
	using System.Windows.Forms;
	using System.Media;

	public partial class ProgramControl : UserControl
	{
		private ProgramStation m_station;
		public ProgramControl(ProgramStation station)
		{
			InitializeComponent();
			this.SizeChanged += new EventHandler(ProgramControl_SizeChanged);
			this.m_station = station;
			this.lbProgramName.Text = station.Name;
			foreach (ProgramShow show in this.m_station.Shows)
			{
				this.lvPrograms.Items.Add(show.CreateListViewItem());
			}
		}

		void ProgramControl_SizeChanged(object sender, EventArgs e)
		{
			this.lvPrograms.Columns[1].Width = this.Width - this.lvPrograms.Columns[0].Width;
		}
		
		private void lvPrograms_Click(object sender, EventArgs e)
		{
			if(this.lvPrograms.SelectedItems.Count == 0)
				return;
			ListViewItem item = this.lvPrograms.SelectedItems[0];
			item.Checked = !item.Checked;
			this.ItemChecked(item);
			
		}

		private void ItemChecked(ListViewItem item)
		{
			if (item == null)
				return;
			
			(item.Tag as ProgramShow).Notify = item.Checked;
		}

		internal void CheckProgram()
		{
			TimeSpan now = DateTime.Now.TimeOfDay;
			if (this.m_station.Date != DateTime.Now.ToShortDateString())
				return;
			foreach (ProgramShow show in this.m_station.Shows)
			{
				if (!show.Notify)
					continue;
				TimeSpan span = show.Time;
				TimeSpan before = new TimeSpan(span.Hours, span.Minutes - 2, 0);
				if (now > before && now < span)
				{
					show.Notify = false;
					MainForm.Notify.BalloonTipTitle = show.Time.ToString();
					MainForm.Notify.BalloonTipText = show.Name;
					MainForm.Notify.BalloonTipIcon = ToolTipIcon.Info;
					MainForm.Notify.ShowBalloonTip(10000);
					SystemSounds.Asterisk.Play();
				}
			}
		}

		private void lvPrograms_ItemChecked(object sender, ItemCheckedEventArgs e)
		{
			this.ItemChecked(e.Item);
		}
	}
}
